﻿(function (w) {
    'use strict';
    var _socket
        , _userId
        , _msg = w.msgTool = {
            init: function (url) {
                _socket = io(url);
            },
            browser: function myfunction() {
                //取得浏览器的userAgent字符串
                var userAgent = navigator.userAgent;
                //判断是否Opera浏览器
                var isOpera = userAgent.indexOf("Opera") > -1;
                //判断是否IE浏览器
                var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera;
                //判断是否IE的Edge浏览器
                var isEdge = userAgent.indexOf("Edge") > -1;
                //判断是否Firefox浏览器
                var isFF = userAgent.indexOf("Firefox") > -1;
                //判断是否Safari浏览器
                var isSafari = userAgent.indexOf("Safari") > -1 && userAgent.indexOf("Chrome") == -1;
                //判断Chrome浏览器
                var isChrome = userAgent.indexOf("Chrome") > -1 && userAgent.indexOf("Safari") > -1;

                if (isIE) {
                    var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
                    reIE.test(userAgent);
                    var fIEVersion = parseFloat(RegExp["$1"]);
                    if (fIEVersion == 7) { return "IE7"; }
                    else if (fIEVersion == 8) { return "IE8"; }
                    else if (fIEVersion == 9) { return "IE9"; }
                    else if (fIEVersion == 10) { return "IE10"; }
                    else if (fIEVersion == 11) { return "IE11"; }
                    else { return "0" }//IE版本过低
                }//isIE end

                if (isFF) { return "FF"; }
                else if (isOpera) { return "Opera"; }
                else if (isSafari) { return "Safari"; }
                else if (isChrome) { return "Chrome"; }
                else if (isEdge) { return "Edge"; }
            },
            //IE10+、Safari和Opera9
            getCurrAbsPathForIE10: function () {
                var a = {}, stack;
                try {
                    a.b();
                }
                catch (e) {
                    stack = e.stack || e.sourceURL || e.stacktrace;
                }
                var rExtractUri = /(?:http|https|file):\/\/.*?\/.+?.js/,
                  absPath = rExtractUri.exec(stack);
                return absPath[0] || '';
            },
            //IE5.5~9
            getCurrAbsPathForIE8: function () {
                var scripts = document.scripts;
                var isLt8 = ('' + document.querySelector).indexOf('[native code]') == -1;
                for (var i = scripts.length - 1, script; script = scripts[i--];) {
                    if (script.readyState == 'interactive') {
                        return isLt8 ? script.getAttribute('src', 4) : script.src;
                    }
                }
            }
        };

    var browserTag = _msg.browser(), url = '';

    switch (browserTag) {
        case 'Chrome':
        case 'FF':
            url = document.currentScript.src;
            break;
        case 'Opera':
        case 'Safari':
        case 'IE10':
            url = _msg.getCurrAbsPathForIE10();
            break;
        case 'IE8':
        case 'IE9':
            url = _msg.getCurrAbsPathForIE8();
            break;
    }
    _msg.init(url.split('/main.js')[0]);


    //******************************客户端调用的方法
    //验证用户
    _msg.join = function (userId) {
        _userId = userId
        _socket.emit('user join', userId);
    }

    //查询消息
    _msg.queryMessage = function (data, callBack) {
        _socket.emit('queryMessage', data, callBack);
    }

    //发送消息
    _msg.sendMessage = function (data, callBack) {
        _socket.emit('sendMessage', data, callBack);
    }

    //更新信息状态
    _msg.updateMessageReadStatus = function (data, callBack) {
        _socket.emit('updateMessageReadStatus', data, callBack);
    }

    //删除数据
    _msg.deleteMessage = function (data, callBack) {
        _socket.emit('deleteMessage', data, callBack);
    }

    //******************************客户端调用的方法


    //******************************接受服务器的消息

    //接收服务端消息
    _socket.on('receiveMessage', function (data, callBack) {
        if (callBack) {
            callBack(data);
        }

        if (_msg.receiveMessageFn) {
            _msg.receiveMessageFn(data);
        }
    });

    //******************************接受服务器的消息

    //当前用户触发焦点.
    w.onfocus = function () {
        _socket.emit('user focus', _userId);
    }
})(window);